/*
 * Copyright 2007, 2011 John A Brown
 * www.nhoj.info           nworbnhoj
 *
 * This file is part of oCSSR.
 *
 * oCSSR is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * oCSSR is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * oCSSR.  If not, see <http://www.gnu.org/licenses/>.
 */

package HMMExplorer;

import java.io.File;
import java.io.Reader;
import java.io.Writer;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFileChooser;

/**
 * HMMStreams is a dialog to allow the selection of the input and output 
 * streams for a Hidden Markov Model.
 *
 * @author nworbnhoj
 */
public class HMMStreams extends javax.swing.JDialog {
    
    private Reader inStream;
    private Writer outStream;
    
    /** Creates new form HMMStreams */
    public HMMStreams(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public Reader getSource(){
        return inStream;
    }
    
     public Writer getDestination(){
        return outStream;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jFileChooser1 = new javax.swing.JFileChooser();
        SourceLabel = new javax.swing.JLabel();
        Source = new javax.swing.JComboBox();
        SourceSelect = new javax.swing.JButton();
        DestinationLabel = new javax.swing.JLabel();
        Destination = new javax.swing.JComboBox();
        DestinationSelect = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        OK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        SourceLabel.setText("Source");

        SourceSelect.setText("Select");
        SourceSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SourceSelectActionPerformed(evt);
            }
        });

        DestinationLabel.setText("Destination");

        DestinationSelect.setText("Select");
        DestinationSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DestinationSelectActionPerformed(evt);
            }
        });

        Cancel.setText("Cancel");

        OK.setText("OK");
        OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(DestinationLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(Destination, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(SourceLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Source, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(OK))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(Cancel)
                    .addComponent(DestinationSelect, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                    .addComponent(SourceSelect, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SourceLabel)
                    .addComponent(Source, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SourceSelect))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DestinationLabel)
                    .addComponent(Destination, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DestinationSelect))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Cancel)
                    .addComponent(OK))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void DestinationSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DestinationSelectActionPerformed
        int returnVal = jFileChooser1.showOpenDialog(HMMStreams.this);
        
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File outFile = jFileChooser1.getSelectedFile();
            
            try {
                this.outStream = new FileWriter(outFile);
                Source.addItem(outFile.getName());
            } catch (java.io.IOException e) {
                Source.addItem("Unable to open selected file");
                this.inStream = null;
            } finally {
            }
        } else {
            //   Open command cancelled by user.
        }
    }//GEN-LAST:event_DestinationSelectActionPerformed
    
    private void SourceSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SourceSelectActionPerformed
        int returnVal = jFileChooser1.showOpenDialog(HMMStreams.this);
        
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File inFile = jFileChooser1.getSelectedFile();
            
            try {
                this.inStream = new FileReader(inFile);
                Source.addItem(inFile);
               // Source.setSelectedItem(inFile);
                
            } catch (java.io.IOException e) {
                Source.addItem("Unable to open selected file");
                this.inStream = null;
            } finally {
            }
        } else {
            //   Open command cancelled by user.
        }
    }//GEN-LAST:event_SourceSelectActionPerformed
    
    private void OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKActionPerformed

    }//GEN-LAST:event_OKActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new HMMStreams(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.swing.JComboBox Destination;
    private javax.swing.JLabel DestinationLabel;
    private javax.swing.JButton DestinationSelect;
    private javax.swing.JButton OK;
    private javax.swing.JComboBox Source;
    private javax.swing.JLabel SourceLabel;
    private javax.swing.JButton SourceSelect;
    private javax.swing.JFileChooser jFileChooser1;
    // End of variables declaration//GEN-END:variables
    
}
